<template>
  <div id="addBlog">
    <h2>添加博客</h2>
    <form v-if="!submited">
      <label>博客标题</label>
      <input type="text" v-model="blog.title">
      <label>博客内容</label>
      <textarea name id cols="30" rows="10" v-model="blog.content"></textarea>
      <div id="checkBox">
        <label for>vue.js</label>
        <input type="checkbox" value="vue.js" v-model="blog.categories">
        <label for>node.js</label>
        <input type="checkbox" value="node.js" v-model="blog.categories">
        <label for>react.js</label>
        <input type="checkbox" value="react.js" v-model="blog.categories">
        <label for>ppt.js</label>
        <input type="checkbox" value="ppt.js" v-model="blog.categories">
      </div>
      <label for>作者：</label>
      <select v-model="blog.author">
        <option v-for="(author,index) in authors" :key="index">{{author}}</option>
      </select>
      <button @click.prevent="post">添加博客</button>
    </form>
    <div>
      <h3 v-if="submited">您的博客发送成功</h3>
    </div>
    <div id="preview">
      <h3>博客纵览：</h3>
      <p>博客标题:{{blog.title}}</p>
      <p>博客内容:</p>
      <p>{{blog.content}}</p>
      <p>博客分类</p>
      <ul>
        <li v-for="(category,index) in blog.categories" :key="index">{{category}}</li>
      </ul>
      <p>作者：{{blog.author}}</p>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
// https://jsonplaceholder.typicode.com/
// https://jsonplaceholder.typicode.com/posts
import axios from "axios";
export default {
  data() {
    return {
      blog: {
        title: "",
        content: "",
        categories: [],
        author: ""
      },
      authors: ["hello", "world"],
      submited: false
    };
  },
  components: {},
  methods: {
    post() {
      axios
        .post("https://wd9328603459zdqdne.wilddogio.com/blog.json", this.blog)
        .then(res => {
          // console.log(res.data);
          this.submited = true;
        })
        .catch(err => console.log(err));
    }
  }
};
</script>

<style scoped>
#addBlog * {
  box-sizing: border-box;
}
#addBlog {
  margin: 20px auto;
  padding: 20px;
  max-width: 600px;
}
label {
  display: block;
  margin: 20px 0 10px;
}
input[type="text"],
textarea,
select {
  display: block;
  width: 100%;
  padding: 10px;
}
#checkBox label {
  display: inline-block;
  margin-top: 4px;
}
#checkBox input {
  margin-right: 10%;
}
button {
  display: block;
  padding: 8px 15px;
  margin: 20px 0;
  background-color: crimson;
  color: #fff;
  cursor: pointer;
  outline: none;
  border: none;
  border-radius: 4px;
  font-size: 16px;
}
#preview {
  margin: 30px 0;
  border: 1px dotted #ccc;
  padding: 20px;
}
h3 {
  margin-top: 20px;
}
</style>
